package com.fingard.dsp.bank.directbank.pab02.Util.api.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author created by pj
 * @date 2018年8月9日上午11:24:28
 * 
 */
public class StreamUtil {

	private static final int BUFFER_SIZE = 1024;

	/**
	 * 输入流转String
	 * 
	 * @param in
	 * @return
	 */
	public static String inputStreamToString(InputStream in) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		String result = null;
		int len = 0;
		try {
			while ((len = in.read(data, 0, BUFFER_SIZE)) != -1) {
				baos.write(data, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		data = null;
		try {
			result = new String(baos.toByteArray(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
 
	/**
	 * 输入流转字节数组
	 * @param in
	 * @return
	 */
	public static byte[] inputStreamToByte(InputStream in) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int len = 0;
		try {
			while ((len = in.read(data, 0, BUFFER_SIZE)) != -1) {
				baos.write(data, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		data = null;
		return baos.toByteArray();
	}

}
